package com.sgcc.ln.pmos.common.check;

import java.security.MessageDigest;

public class MD5Util {
    private static final String[] hexDigIts = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public MD5Util() {
    }

    public static String md5Encode(String origin) {
        return md5Encode(origin, "utf-8");
    }

    public static String md5Encode(String origin, String charsetName) {
        String resultString = null;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (null != charsetName && !charsetName.isEmpty()) {
                resultString = byteArrayToHexString(md.digest(origin.getBytes(charsetName)));
            } else {
                resultString = byteArrayToHexString(md.digest(origin.getBytes()));
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return resultString;
    }

    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();

        for(int i = 0; i < b.length; ++i) {
            resultSb.append(byteToHexString(b[i]));
        }

        return resultSb.toString();
    }

    public static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigIts[d1] + hexDigIts[d2];
    }
}
